package com.gls.starter.core.support;

import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;

import java.io.IOException;
import java.util.List;

/**
 * @author lizhiyong
 */
public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException {

        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();

        Resource resource = encodedResource.getResource();

        List<PropertySource<?>> propertySources = loader.load(resource.getFilename(), resource);

        return propertySources.get(0);
    }
}
